package com.nega.modules.thread;

import com.nega.commons.model.SearchResult;
import com.nega.commons.spider.IPSourcePool;
import com.nega.commons.spider.SpiderCoreService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/13 18:36
 * @Description: what it is class?
 */
@Slf4j
public class SpiderThreadBaidu implements Callable<List<SearchResult>> {

    private              SpiderCoreService spiderCoreService;
    private              IPSourcePool      ipSourcePool;
    private              int               num;
    private              String            text;
    private static final String            DOMAIN  = "https://www.baidu.com";
    private static final String            CONTEXT = "/s";

    public SpiderThreadBaidu() {

    }

    public SpiderThreadBaidu(SpiderCoreService spiderCoreService, IPSourcePool ipSourcePool, String text, int num) {
        this.num = num;
        this.spiderCoreService = spiderCoreService;
        this.text = text;
        this.ipSourcePool = ipSourcePool;
    }

    @Override
    public List<SearchResult> call() {
        List<SearchResult> results = new ArrayList<>();
        Map<String, String> params = getParams(text);
        int size = (num * 10);
        String ip = ipSourcePool.getIp();
        for (int i = (size - 10); i < size; i++) {
            params.put("pn", String.valueOf(i * 10));
            Document document = spiderCoreService.getDocument(DOMAIN + CONTEXT, params, ip);
            results.addAll(getInfo(document, i));
            if (isLastPage(document)) {
                break;
            }
        }
        return results;
    }

    private Map<String, String> getParams(String text) {
        Map<String, String> params = new HashMap<>();
        params.put("ie", "utf-8");
        params.put("newi", "1");
        params.put("mod", "1");
        params.put("isbd", "1");
        params.put("isid", "eb4c986f000015ad");
        params.put("wd", text);
        params.put("oq", text);
        params.put("tn", "baiduhome_pg");
        params.put("usm", "1");
        params.put("rsv_idx", "2");
        params.put("rsv_pq", "eb4c986f000015ad");
        params.put("rsv_t", "ce42oJFgXZUO8ApW3Uz%2FJwjv%2Fe0SIfZIiNDxlFk84vpMPGKnmmzYW8KGR01%2F2l0SST3P");
        params.put("bs", text);
        params.put("rsv_sid", "26522_1451_21105_28767_28721_28964_28837_28584_26350_28603");
        params.put("_ss", "1");
        params.put("clist", "");
        params.put("hsug", "");
        params.put("f4s", "1");
        params.put("csor", "6");
        params.put("_cr1", "32242");
        return params;
    }

    private boolean isLastPage(Document document) {
        if (document == null) {
            return true;
        }
        try {
            Element element = document.getElementById("page");
            if (element == null){
                return true;
            }
            Elements elements = element.getElementsByAttributeValue("class", "n");
            for (Element ele : elements) {
                if ("下一页>".equals(ele.text())) {
                    return false;
                }
            }
        } catch (Exception e) {
            log.error("百度检测下一页异常", e);
        }
        return true;
    }

    private List<SearchResult> getInfo(Document document, int page) {
        List<SearchResult> results = new ArrayList<>();
        if (document == null) {
            return results;
        }
        Element element = document.getElementById("content_left");
        Elements resultc = element.children();
        for (int i = 0; i < resultc.size(); i++) {
            Element elementf = resultc.get(i);
            if (!"div".equals(elementf.tagName())) {
                continue;
            }
            try {

                Elements guangao = elementf.getElementsByClass("ec_tuiguang_container");
                if (guangao.size() > 0) {
                    continue;
                }
                Element a = elementf.getElementsByTag("a").get(0);
                String title = a.text();
                if (StringUtils.isBlank(title)){
                    continue;
                }
                String url = a.attr("href");
                if (title.contains("百度") && !(title.contains("贴吧") || title.contains("知道"))) {
                    continue;
                }

                if (title.contains("最新相关")) {
                    Elements elements = elementf.getElementsByClass("c-gap-bottom-small");
                    String smallTitle = "";
                    String smallurl ="";
                    if (elements.size()>1){
                        Element row = elements.get(0);
                        Element rowa = row.getElementsByTag("a").get(0);
                        smallTitle = rowa.text();
                        smallurl = rowa.attr("href");
                    }
                    for (Element row : elementf.getElementsByClass("c-row")) {
                        SearchResult rowresult = new SearchResult();
                        rowresult.setKeyWord(text);
                        if (row.hasClass("c-gap-bottom-small")){
                            rowresult.setTitle(smallTitle);
                            rowresult.setUrl(smallurl);
                            Element spanLast = row.getElementsByClass("c-span-last").get(0);
                            rowresult.setContent(spanLast.text());
                            Elements spans = spanLast.getElementsByTag("span");
                            if (spans.size() > 0) {
                                rowresult.setTime(spans.get(1).text());
                                rowresult.setSource(spans.get(0).text());
                            }
                        }else {
                            Element rowa = row.getElementsByTag("a").get(0);
                            String rowtitle = rowa.text();
                            String rowurl = rowa.attr("href");
                            rowresult.setTitle(rowtitle);
                            rowresult.setUrl(rowurl);
                            Elements spans = row.getElementsByTag("span");
                            if (spans.size() > 0) {
                                rowresult.setTime(spans.get(0).text());
                                rowresult.setSource(spans.get(1).text());
                            }
                        }
                        results.add(rowresult);
                    }
                    continue;
                }
                SearchResult result = new SearchResult();
                result.setKeyWord(text);
                result.setTitle(title);
                result.setUrl(url);

                Elements contents = elementf.getElementsByAttributeValue("class", " c-abstract");
                if (contents != null && contents.size() > 0) {
                    result.setContent(contents.get(0).text());
                }

                Elements times = elementf.getElementsByAttributeValue("class", " newTimeFactor_before_abs m");
                if (times.size() > 0) {
                    Element time = times.get(0);
                    String date = time.text();
                    result.setTime(date);
                }

                Elements f13s = elementf.getElementsByAttributeValue("class", "f13");
                if (f13s.size() > 0) {
                    Element f13 = f13s.get(0);
                    Elements spans = f13.getElementsByTag("span");
                    if (spans.size() > 0) {
                        for (Element span : spans) {
                            if (!span.hasAttr("class")) {
                                result.setSource(span.text());
                            }
                        }
                    }
                }
                results.add(result);
            } catch (Exception e) {
                log.error("百度抓取第[{}]页，显示排位第[{}]位异常", page, i, e);
            }
        }
        return results;
    }

}
